package tpdisenio2009.model.dao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import tpdisenio2009.model.HibernateUtils;
import tpdisenio2009.model.beans.User;


public class UserDAO extends AbstractDAO<User> {

    @Override
    public Class<User> getPersistentClass() {
        return User.class;
    }

    public User getByName( String firstName, String lastName ) {
        Session session = HibernateUtils.getCurrentSession();

        Criterion firstNameCriterion = Property.forName( "firstName" ).eq( firstName );
        Criterion lastNameCriterion = Property.forName( "lastName" ).eq( lastName );

        LogicalExpression nameExpression = Restrictions.and( firstNameCriterion, lastNameCriterion );

        Criteria criteria = session.createCriteria( User.class ).add( nameExpression );

        return (User) criteria.uniqueResult();
    }

    public User getUserByDocument( String document, String documentType ) {
        Session session = HibernateUtils.getCurrentSession();

        Criterion documentCriterion = Property.forName( "document" ).eq( document );
        Criterion documentTypeCriterion = Property.forName( "documentType" ).eq( documentType );

        LogicalExpression documentExpression = Restrictions.and( documentCriterion, documentTypeCriterion );

        Criteria criteria = session.createCriteria( User.class ).add( documentExpression );

        return (User) criteria.uniqueResult();
    }

}
